package com.tsing.service.impl;

import com.tsing.common.result.PageResult;
import com.tsing.common.result.Result;
import com.tsing.mapper.*;
import com.tsing.mapper.impl.*;
import com.tsing.pojo.entity.*;
import com.tsing.pojo.vo.ArticleDetailsVO;
import com.tsing.pojo.vo.CommentDetailsVO;
import com.tsing.pojo.vo.DetailsVo;
import com.tsing.pojo.vo.DynamicVO;
import com.tsing.service.DynamicService;

import java.util.ArrayList;
import java.util.List;

/**
 * @auth 卢孝宇
 * @date 2023/12/13 17:07
 */
public class DynamicServiceImpl implements DynamicService {

    private ArticleMapper articleMapper = new ArticleMapperImpl();

    private DynamicMapper dynamicMapper = new DynamicMapperImpl();

    private CollectMapper collectMapper = new CollectMapperImpl();

    private PostDetailsMapper postDetailsMapper = new PostDetailsMapperImpl();

    private LoveMapper loveMapper = new LoveMapperImpl();
    @Override
    public PageResult<DynamicVO> dynamic(Integer userId, Integer page, Integer size) {
        PageResult<DynamicVO> apArticleVOList = new PageResult<>();
        List<DynamicVO> list = new ArrayList<>();
        //根据登录用户id查询关注的用户id
        List<TlInterestid> userList = dynamicMapper.userList(userId);

        List<Long> userIdList = new ArrayList<>();

        //根据查询出来的用户id去一个个查询对应的所有发帖数据
        for (TlInterestid user : userList) {
            userIdList.add(user.getUserId());
        }
        List<TlPost> postList = dynamicMapper.postList(userIdList.toArray());

        //查询用户关注用户发布的帖子的所有数量
        Integer count = dynamicMapper.countAll(userIdList.toArray());

        for (TlPost tlPost : postList) {

            DynamicVO a = new DynamicVO();
            //查看是否点赞
            List<TlLove> tlLoves = loveMapper.loveCount(Long.parseLong(userId.toString()), tlPost.getPostId());
            //查看是否收藏
            Integer collectCounts = collectMapper.collectCount(userId,tlPost.getPostId());
            a.setIfCollect(collectCounts>0);
            a.setIfLove(!tlLoves.isEmpty());
            a.setPostId(tlPost.getPostId());
            a.setTitle(tlPost.getTitle());
            a.setLikeCount(tlPost.getLikeCount());
            a.setDetailsImage(tlPost.getImage());
            a.setDetailsId(tlPost.getDetailsId());
            a.setUserId(tlPost.getUserId());
            a.setCreateDate(tlPost.getCreateDate());
            a.setCopywriting(tlPost.getCopywriting());

            //根据用户id查找对应的用户数据
            List<TlUser> tlUsers = articleMapper.userList(tlPost.getUserId());
            //获取收藏数
           Integer collectCount = dynamicMapper.collectCount(tlPost.getPostId());
           a.setCollections(collectCount);
            for (TlUser tlUser : tlUsers) {
                a.setUserName(tlUser.getUsername());
                a.setUserImage(tlUser.getUserImage());
            }
            list.add(a);
        }
        apArticleVOList.setData(list);
        apArticleVOList.setCode(200);
        apArticleVOList.setSize(size);
        apArticleVOList.setTotal(count);
        apArticleVOList.setCurrentPage(page);
        return apArticleVOList;
    }

    @Override
    public Result<DetailsVo> detailsList(Integer postId,Integer userId) {

        //根据帖子id查询对应的帖子基本数据
        TlPost post = articleMapper.getPostById(postId);

        DetailsVo detailsVo = new DetailsVo();
        detailsVo.setTitle(post.getTitle());
        detailsVo.setSpecific(post.getCopywriting());
        detailsVo.setDate(post.getCreateDate());
        List<CommentDetailsVO> listComment = new ArrayList<>();
        //根据帖子id查询对应的帖子评论信息
        List<TlDetails> list = postDetailsMapper.list(postId);
        for (TlDetails tlDetails : list) {
            CommentDetailsVO commentDetailsVO = new CommentDetailsVO();
            //根据评论id查询对应的评论用户信息
            TlUser user = postDetailsMapper.userById(tlDetails.getUserId());
            commentDetailsVO.setUserName(user.getUsername());
            commentDetailsVO.setUserImage(user.getUserImage());
            commentDetailsVO.setComments(tlDetails.getComments());
            commentDetailsVO.setAddress(tlDetails.getAddress());
            commentDetailsVO.setRecoveryTime(tlDetails.getRecoveryTime());
            commentDetailsVO.setDetailsId(tlDetails.getDetailsId());
            //根据评论id查询对应的点赞数
            Integer i = postDetailsMapper.likeCount(tlDetails.getDetailsId());
            commentDetailsVO.setLikeCount(Long.parseLong(i.toString()));
            Boolean flag = postDetailsMapper.flag(tlDetails.getDetailsId(), userId);
            commentDetailsVO.setFlag(flag);
            listComment.add(commentDetailsVO);
        }
        detailsVo.setComment(listComment);
        return Result.success(detailsVo);
    }
}
